// miniprogram/pages/mine/setting/typeManage/typeDetail/typeDetail.js
const app = getApp()
const util = require('../../../../../utils/util.js')
const common = require('../../../../../utils/common.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isFullSucreen: app.globalData.isFullSucreen,
    type:'',
    isEdit:false,
    typeDetail:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    if (options.type) {
      if(options.detail){
        that.setData({
          typeDetail:JSON.parse(options.detail)
        })
        switch (options.type) {
          case 'member':
            wx.setNavigationBarTitle({
              title: '编辑成员',
            })
            break;
          case 'expendType':
            wx.setNavigationBarTitle({
              title: '编辑支出类型',
            })
            break;
          case 'incomeType':
            wx.setNavigationBarTitle({
              title: '编辑收入类型',
            })
            break;
          case 'accountType':
            wx.setNavigationBarTitle({
              title: '编辑账户',
            })
            break;
          default:
            break;
        }
      }else{
        switch (options.type) {
          case 'member':
            wx.setNavigationBarTitle({
              title: '新建成员',
            })
            break;
          case 'expendType':
            wx.setNavigationBarTitle({
              title: '新建支出类型',
            })
            break;
          case 'incomeType':
            wx.setNavigationBarTitle({
              title: '新建收入类型',
            })
            break;
          case 'accountType':
            wx.setNavigationBarTitle({
              title: '新建账户',
            })
            break;
          default:
            break;
        }
      }
      that.setData({
        type: options.type,
        isEdit:options.detail?true:false
      })
      console.log(that.data.type)
      console.log(that.data.isEdit)
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  nameInput: function (e) {
    var that = this
    var typeDetail = that.data.typeDetail
    typeDetail.name = e.detail.value
    that.setData({
      typeDetail:typeDetail
    })
  },
  remarkInput: function (e) {
    var that = this
    var typeDetail = that.data.typeDetail
    typeDetail.remark = e.detail.value
    that.setData({
      typeDetail:typeDetail
    })
  },
  saveClick: function () {
    var that = this
    var mode = 'save'
    if(that.data.isEdit){
      mode = 'modify'
      wx.showLoading({
        title: '正在修改',
      })
    }else{
      wx.showLoading({
        title: '正在删除',
      })
    }
    wx.request({
      url: `${common.baseUrl()}/${that.data.type}/${mode}`,
      method: that.data.isEdit?'PUT':'POST',
      header: {
        // "Content-Type": "application/x-www-form-urlencoded",
        "Authorization": app.globalData.token
      },
      data: that.data.typeDetail,
      success(res) {
        wx.hideLoading()
        console.log(res)
        if (res.data.flag) {
          //新增修改后，退出界面，刷新上界面
          var pages = getCurrentPages(); //当前页面栈
          wx.navigateBack()
          if (pages.length > 1) {
            var beforePage = pages[pages.length - 2] //获取上一个页面实例对象
            beforePage.onPullDownRefresh() //触发父页面中的方法
          }
        }
      },
      fail(res) {
        wx.hideLoading()
        console.log(res)
      }
    })
  },
  deleteClick: function () {
    var that = this
    wx.showLoading({
      title: '正在删除',
    })
    wx.request({
      url: `${common.baseUrl()}/${that.data.type}/remove/${that.data.typeDetail.id}`,
      method: "DELETE",
      header: {
        // "Content-Type": "application/x-www-form-urlencoded",
        "Authorization": app.globalData.token
      },
      // data: {
      //   userId: app.globalData.userInfo.id
      // },
      success(res) {
        wx.hideLoading()
        console.log(res)
        if (res.data.flag) {
          //删除后，退出界面，刷新上界面
          var pages = getCurrentPages(); //当前页面栈
          wx.navigateBack()
          if (pages.length > 1) {
            var beforePage = pages[pages.length - 2] //获取上一个页面实例对象
            beforePage.onPullDownRefresh() //触发父页面中的方法
          }
        }else{
          wx.showToast({
            title: res.data.message,
            image: '/images/warning.png'
          })
        }
      },
      fail(res) {
        console.log(res)
      }
    })
  }
})